package com.jboltai.capability.text.extract.pdf;

import com.jboltai.capability.text.ContentInfo;
import com.jboltai.capability.text.ContentType;
import org.apache.pdfbox.contentstream.PDFStreamEngine;
import org.apache.pdfbox.contentstream.operator.DrawObject;
import org.apache.pdfbox.contentstream.operator.Operator;
import org.apache.pdfbox.contentstream.operator.state.*;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState;
import org.apache.pdfbox.util.Matrix;

import java.io.IOException;
import java.util.List;

public class ImageLocations extends PDFStreamEngine {

    public ImageLocations() throws IOException {
        // Prepare PDFStreamEngine
        addOperator(new Concatenate());
        addOperator(new DrawObject());
        addOperator(new SetGraphicsStateParameters());
        addOperator(new Save());
        addOperator(new Restore());
        addOperator(new SetMatrix());
    }

    List<ContentInfo> contentInfos;

    float height = 0f;

    public List<ContentInfo> processPage(PDPage page, List<ContentInfo> contentInfos) throws IOException {
        this.height = page.getMediaBox().getHeight();
        this.contentInfos = contentInfos;
        this.processPage(page);
        return this.contentInfos;
    }

    @Override
    protected void processOperator(Operator operator, List<COSBase> operands) throws IOException {
        String operation = operator.getName();
        if ("Do".equals(operation)) {
            COSName objectName = (COSName) operands.get(0);
            PDResources resources = this.getResources();
            if (resources.isImageXObject(objectName)) {
                PDImageXObject image = (PDImageXObject) resources.getXObject(objectName);
                PDGraphicsState graphicsState = getGraphicsState();
                Matrix ctmNew = graphicsState.getCurrentTransformationMatrix();
//                float width = ctmNew.getScalingFactorX();
//                float height = ctmNew.getScalingFactorY();
                float x = ctmNew.getTranslateX();
                //图片是从底部向上渲染的
                float y = height - ctmNew.getTranslateY() ;

                boolean insertSuccess = false;
                for (int i = 0; i < contentInfos.size(); i++) {
                    if (contentInfos.get(i).getY() >= y) {
                        contentInfos.add(i, new ContentInfo(ContentType.IMG, image,x,y ));
                        insertSuccess = true;
                        break;
                    }
                }

                if (insertSuccess == false) {
                    //所有文本都在它上面
                    contentInfos.add(new ContentInfo(ContentType.IMG, image,x,y ));
                }

            }
        } else {
            super.processOperator(operator, operands);
        }
    }
}
